import * as server from "@minecraft/server";
import * as data_block from "../data/block";
import * as realm_property from "../system/realm_property";
import * as native_tools from "../system/native_tools";
import * as achieve_plan from "../system/achieve_plan";
import * as math_tools from "../system/math_tools";
export function Forestry(block) {
    if (!realm_property.ExpendEnergy(block, -50))
        return;
    const target = block.south();
    if (!target || !target.isValid())
        return;
    const test = data_block.is_wood.has(target.typeId);
    if (!test)
        return;
    switch (target?.typeId) {
        case 'minecraft:dark_oak_log':
            native_tools.TryFillBlocks(block.dimension, math_tools.Vector.add(block, { x: 1, y: 0, z: 2 }), math_tools.Vector.add(block, math_tools.Vector.CONSTANT_SOUTH), 'minecraft:dark_oak_sapling');
            break;
        case 'minecraft:oak_log':
            target.setPermutation(server.BlockPermutation.resolve('minecraft:oak_sapling'));
            break;
        case 'minecraft:spruce_log':
            target.setPermutation(server.BlockPermutation.resolve('minecraft:spruce_sapling'));
            break;
        case 'minecraft:birch_log':
            target.setPermutation(server.BlockPermutation.resolve('minecraft:birch_sapling'));
            break;
        case 'minecraft:jungle_log':
            target.setPermutation(server.BlockPermutation.resolve('minecraft:jungle_sapling'));
            break;
        case 'minecraft:acacia_log':
            target.setPermutation(server.BlockPermutation.resolve('acacia_sapling'));
            break;
        case 'minecraft:cherry_log':
            target.setPermutation(server.BlockPermutation.resolve('minecraft:cherry_sapling'));
            break;
        case 'minecraft:crimson_stem':
            target.setPermutation(server.BlockPermutation.resolve('minecraft:crimson_fungus'));
            break;
        case 'minecraft:warped_stem':
            target.setPermutation(server.BlockPermutation.resolve('minecraft:warped_fungus'));
            break;
        default: break;
    }
    ;
    const start = math_tools.Vector.add(block, { x: -7, y: -1, z: -6 });
    const done = math_tools.Vector.add(block, { x: 7, y: 19, z: 8 });
    const moveEvent = (args) => {
        const TestSort = () => {
            const getBlock = args.dimension.getBlock(args.location);
            if (getBlock)
                return data_block.is_trees.has(getBlock.typeId);
            else
                return false;
        };
        if (TestSort())
            args.dimension.runCommand(`fill ${args.location.x} ${args.location.y} ${args.location.z} ${args.location.x} ${args.location.y} ${args.location.z} air [] destroy`);
        return true;
    };
    achieve_plan.PathExecute.CreateForCube('植树造木-范围扫描', {
        particles: ['constant:track_rune_green'],
        dimension: block.dimension,
        location_group: [],
        cooldown: 1,
        speed: 1,
        offset: math_tools.Vector.CONSTANT_HALF,
        on_move: moveEvent
    }, start, done, 0.5);
}
;
export function Detection(object) {
    if (!realm_property.ExpendEnergy(object, -5))
        return;
    const TickEvent = (args) => {
        const getBlock = args.dimension.getBlock(args.location);
        if (!getBlock || !getBlock.isValid())
            return false;
        const protoResult = data_block.is_crops.get(getBlock.typeId);
        const getPermutation = getBlock.permutation;
        if (protoResult === true || getPermutation.getState('growth') == 7)
            return false;
        else if (getBlock.typeId == 'minecraft:sweet_berry_bush' && getPermutation.getState('growth') == 3)
            return false;
        else if (getBlock.typeId == 'minecraft:cocoa' && getPermutation.getState('age') == 2)
            return false;
        else if (getBlock.typeId == 'nether_wart' && getPermutation.getState('age') == 3)
            return false;
        else
            return true;
    };
    const StopEvent = (args) => {
        if (args.tick > 15)
            return;
        const getBlock = object.north();
        if (!getBlock || !getBlock.isValid())
            return;
        const getPermutation = getBlock.permutation;
        try {
            getBlock.setPermutation(getPermutation.withState('STATE:rune_type', math_tools.Random({ max: 7, min: 1 }, true)));
        }
        catch { }
    };
    achieve_plan.PathExecute.Create('作物侦测-检测射线', 1, {
        location_group: [object, math_tools.Vector.add(object, { x: 0, y: 0, z: 15 })],
        particles: ['constant:track_rune_green'],
        offset: { x: 0.5, y: 0.5, z: 0.5 },
        dimension: object.dimension,
        on_move: TickEvent,
        on_done: StopEvent,
        cooldown: 1,
        speed: 1
    });
}
;
